import MindMap from "simple-mind-map";
import Export from "simple-mind-map/src/plugins/Export.js";
MindMap.usePlugin(Export);

const getFirstActiveNode = (activeNodes) =>
  activeNodes ? activeNodes[0] : null;

//插入子节点
const insertChildNode = (mindMap) => {
  mindMap.execCommand("INSERT_CHILD_NODE", false);
};

//插入同级节点
const insertSiblingNode = (mindMap, activeNodes) => {
  const activeNode = getFirstActiveNode(activeNodes);
  if(!activeNode.isRoot){
    mindMap.execCommand("INSERT_NODE", false);
  }
};

const getParentNodeOfActiveNodes = (activeNodes) => {
  const activeNode = getFirstActiveNode(activeNodes);
  return activeNode ? activeNode.parent : null;
};

const getSiblingNodesOfActiveNodes = (activeNodes) => {
  const parentNode = getParentNodeOfActiveNodes(activeNodes);
  return parentNode ? parentNode.children : null
}

//激活父节点
const activeParentNode = (mindMap, activeNodes) => {
  const parentNode = getParentNodeOfActiveNodes(activeNodes);
  if (parentNode) {
    parentNode.active();
  }
};

//激活子节点
const activeChildNode = (mindMap, activeNodes) => {
  const activeNode = getFirstActiveNode(activeNodes);
  if (activeNode) {
    const childNode = activeNode.children ? activeNode.children[0] : null;
    if (childNode) {
      childNode.active();
    }
  }
};

//激活上一个节点
const activePreNode = (mindMap, activeNodes) => {
  const activeNode = getFirstActiveNode(activeNodes);
  if(activeNode){
    const siblingNodes = getSiblingNodesOfActiveNodes(activeNodes)
    let preNode = activeNode
    for(const node of siblingNodes){
      if(node.uid === activeNode.uid){
        break
      }
      preNode = node
    }
    preNode.active()
  }
};

//激活下一个节点
const activeNextNode = (mindMap, activeNodes) => {
  const activeNode = getFirstActiveNode(activeNodes);
  if(activeNode){
    const siblingNodes = getSiblingNodesOfActiveNodes(activeNodes)
    let found = false;
    let nextNode = activeNode
    for(const node of siblingNodes){
      if(found){
        nextNode = node;
        break;
      }
      if(node.uid === activeNode.uid){
        found = true
      }
    }
    nextNode.active()
  }
};

const isTextEdit = (mindMap) => {
  return mindMap.renderer.textEdit.isShowTextEdit();
};

//编辑节点
const editNode = (mindMap, activeNodes) => {
  const node = getFirstActiveNode(activeNodes);
  if (node) {
    const textEdit = mindMap.renderer.textEdit;
    textEdit.show({ node });
  }
};

//取消编辑节点
const cancelEditNode = (mindMap) => {
  mindMap.renderer.textEdit.hideEditTextBox();
};

const shortcuts = {
  Tab: (mindMap, activeNodes, event) => {
    if (!isTextEdit(mindMap)) {
      insertChildNode(mindMap);
      event.preventDefault();
    }
  },
  Enter: (mindMap, activeNodes, event) => {
    if (isTextEdit(mindMap)) {
      cancelEditNode(mindMap);
    } else {
      insertSiblingNode(mindMap, activeNodes);
    }
    event.preventDefault();
  },
  ArrowUp: (mindMap, activeNodes, event) => {
    if (!isTextEdit(mindMap)) {
      activePreNode(mindMap, activeNodes);
      event.preventDefault();
    }
  },
  ArrowDown: (mindMap, activeNodes, event) => {
    if (!isTextEdit(mindMap)) {
      activeNextNode(mindMap, activeNodes);
      event.preventDefault();
    }
  },
  ArrowLeft: (mindMap, activeNodes, event) => {
    if (!isTextEdit(mindMap)) {
      activeParentNode(mindMap, activeNodes);
      event.preventDefault();
    }
  },
  ArrowRight: (mindMap, activeNodes, event) => {
    if (!isTextEdit(mindMap)) {
      activeChildNode(mindMap, activeNodes);
      event.preventDefault();
    }
  },
  " ": (mindMap, activeNodes, event) => {
    if (!isTextEdit(mindMap)) {
      editNode(mindMap, activeNodes);
      event.preventDefault();
    }
  },
};

export default class MyMindMap {
  constructor(id) {
    this.mindMap = new MindMap({
      el: document.getElementById(id),
      data: {
        data: {
          text: "根节点",
        },
        children: [],
      },
    });
    //激活的节点
    this.activeNodes = [];
    this.init();
  }

  //取消默认快捷键
  disableDefaultShortcuts() {
    this.mindMap.keyCommand.pause();
  }

  //注册快捷键
  registerShortcuts() {
    this.onKeyDown = e => {
      const fn = shortcuts[e.key];
      const { mindMap, activeNodes } = this;
      if (fn) {
        fn(mindMap, activeNodes, e);
      }
    }
    document.addEventListener("keydown", this.onKeyDown);
  }

  //取消快捷键
  unregisterShortcuts() {
    document.removeEventListener("keydown", this.onKeyDown);
  }

  init() {
    this.disableDefaultShortcuts();
    this.registerShortcuts();
    this.mindMap.on("node_active", (node, nodeList) => {
      this.activeNodes = nodeList;
    });
    this.mindMap.on('beforeDestroy', () => {
      this.unregisterShortcuts()
    })
    let first = true;
    this.mindMap.on('node_tree_render_end', () => {
      if(first){
        first = false
        const root = this.mindMap.renderer.root;
        root.active()
      }
      
    })
  }

  //获取数据
  getData() {
    return this.mindMap.getData(false);
  }

  //设置数据
  setData(data) {
    return this.mindMap.setData(data);
  }
  //获取当前激活的节点
  getActiveNode(){
    return getFirstActiveNode(this.activeNodes)
  }
}
